/*
 * @Author: shengduiliang 1107722390@qq.com
 * @Date: 2024-04-28 00:05:46
 * @LastEditors: shengduiliang 1107722390@qq.com
 * @LastEditTime: 2024-04-28 01:26:36
 * @FilePath: /treasury/src/hooks/useDebounce.ts
 * @Description: 防抖hook
 */
import { useEffect, useState } from "react"

/**
 * 防抖hook
 * @param value 值
 * @param delay 防抖时间, 默认500ms
 * @returns 
 */
export const useDebounce = <T>(value: T, delay: number = 500) => {
  const [debouncedValue, setDebouncedValue] = useState<T>(value)

  useEffect(() => {
    const handler = setTimeout(() => {
      setDebouncedValue(value);
    }, delay)
  
    return () => {
      clearTimeout(handler)
    };
  }, [value, delay])
  
  return debouncedValue
}